﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using COFE.Core;
using COFE.Core.Profile;
using COFE.Core.Utils;

namespace COFE.Core.Lister
{
    public abstract class PrimaryDirectoryListerBase<T> : DirectoryListerBase<T>, IPrimaryDirectoryLister
    {
       #region Constructors

        protected PrimaryDirectoryListerBase(IFactory<IDirectoryLister> factory, string listerKey)
            : base(factory, listerKey)
        {
            Status = DirectoryListerStatusMode.Fixed;
        }

        #endregion

        #region Methods        

        public virtual IFileSystemInfo ParsePath(string path, EntryConstructionFlags flags)
        {
            T parsedItem = _parse(path);

            if (parsedItem != null)
            {
                var parent = _getParent(parsedItem);                
                string parentParsePath = 
                    parent == null ? null : CreatePropertyProvider(parent).GetParseName();
                return COFEShared.EntryConstructor.CreateEntry(ListerKey, 
                    CreatePropertyProvider(parsedItem), flags);
            }

            return null;
        }

        public override IEnumerable<IFileSystemInfo> List(IInternalDirectoryInfoExA parent, EntryConstructionFlags flags)
        {
            string parentParseName = parent.GetParseNameOrDefault();
            T parentEntry = getEmbeddedItem(parent);
            IEnumerator<T> etor = _list(getEmbeddedItem(parent)).GetEnumerator();
            while (etor.MoveNext())
                yield return COFEShared.EntryConstructor.CreateEntry(ListerKey,
                    CreatePropertyProvider(etor.Current), flags);
        }

        #endregion

   
        #region Data

        protected Func<T[]> _getRootDirectory = () => { return new T[] { }; };
        protected Func<string[]> _getSupportedPathPatterns = () => { return new string[] { }; };
        protected Func<string> _getTempPath = () => { return null; };
        protected Func<string, T> _parse = (path) => { return default(T); };

        #endregion

        #region Public Properties
        public virtual DirectoryListerStatusMode Status { get; protected set; }

        public IDirectoryInfo[] RootDirectories
        {
            get
            {
                return _getRootDirectory()
                    .Select((r) => { return COFEShared.EntryConstructor.CreateDirectory(ListerKey, 
                        CreatePropertyProvider(r), EntryConstructionFlags.ConvertEntry); })
                    .Where((e, p) => { return e is IInternalDirectoryInfoExA; })
                    .Select((e, p) => { return e as IInternalDirectoryInfoExA; }).ToArray(); ;
            }
        }
        public string[] SupportedPathPatterns { get { return _getSupportedPathPatterns(); } }
        public string RootTempPath { get { return _getTempPath(); } }        
        
        #endregion
    }
}
